package jsyf.oa.web.yuce;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ExportWeekExcel;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.entity.system.CompanyStockEntity;
import jsyf.oa.facade.yuce.YuceMainFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Lenovo on 2017/12/11.
 */
@Api(value = "", description = "")
@Controller
@RequestMapping("/yuceMainController")
public class YuceMainController {

    @Autowired
    private YuceMainFacade yuceMainFacade;


    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        Boolean result = yuceMainFacade.save(jsonData);

        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond());

    }

    @RequestMapping(value = "/select.json", method = RequestMethod.POST)
    public void select(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonObject = yuceMainFacade.select(jsonData);

        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));

    }

    @RequestMapping(value = "/getDownByCondition.json")
    public void getDownByCondition(HttpServletResponse response, @RequestParam String companyId, @RequestParam String date, @RequestParam String regionId, @RequestParam String userId, @RequestParam String title) throws Exception {
        String jsonData="{\"companyId\":\""+companyId+"\",\"date\":\"" +date + "\",\"regionId\":\"" +regionId + "\",\"userId\":\"" + userId+ "\"}";
        JSONObject result=yuceMainFacade.select(jsonData);
        JSONObject yuce=result.getJSONObject("main");
        List<JSONObject> models = (List<JSONObject>) net.sf.json.JSONArray.toCollection(net.sf.json.JSONArray.fromObject(result.getString("sub")), JSONObject.class);
        List<JSONObject> newModels=new ArrayList<JSONObject>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int currentMonth = Integer.parseInt(sdf.format(new Date()).split("-")[1]);
/**
 * 文件名
 */
        JSONObject Text=yuceMainFacade.getTitle(companyId,regionId);
        title=Text.getString("companyName")+"-"+Text.getString("regionName");
        if(StringUtils.isBlank(regionId)){
            title=Text.getString("companyName");
        }
        //拼接标题前两行
        JSONObject company=new JSONObject();
        JSONObject model=new JSONObject();
        company.put("companyName",Text.getString("companyName"));
        company.put("modelName","");
        newModels.add(company);
        model.put("modelName","机型");
        model.put("firstMonth",String.valueOf(currentMonth));
        model.put("secondMonth",String.valueOf((currentMonth + 1) % 12 == 0 ? 12 : (currentMonth + 1) % 12));
        model.put("thirdMonth",String.valueOf((currentMonth + 2) % 12 == 0 ? 12 : (currentMonth + 2) % 12));
        newModels.add(model);
        for(JSONObject object:models){
            newModels.add(object);
        }

        export(title, newModels,yuce, response);
    }

    public void export(String title, List<JSONObject> models,JSONObject yuce, HttpServletResponse response) throws Exception {
        // String result = yuceMainFacade.export(jsonData);
        ExportWeekExcel excel = new ExportWeekExcel(title, models,yuce,response);
        excel.creat2003Excel();
    }

    @RequestMapping(value = "/applyAudit.json", method = RequestMethod.POST)
    public void applyAudit(HttpServletResponse response, @RequestBody String jsonData) {
        Boolean result = yuceMainFacade.applyAudit(jsonData);

        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond());

    }

    /**
     * 获取所有公司
     */
    @ApiOperation(
            value = "获取公司---区域",
            notes = "获取公司---区域",
            httpMethod = "POST",
            response = ResponseUtils.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonData", value = "", dataType = "参数类型（string）", paramType = "参数类型（json）")
    })
    @ResponseBody
    @RequestMapping(value = "/getCompanyRegionTree.json", method = RequestMethod.POST)
    public void getCompanyRegionTree(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> records = this.yuceMainFacade.getCompanyRegionTree(jsonData);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }
    @RequestMapping(value = "/getFridayList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取周五预测",
            notes = "获取周五预测",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData",value = "{id:''}",dataType = "String", paramType = "body")
    public void getFridayList(HttpServletResponse response,@RequestBody String jsonData) {
        List<JSONObject> result = this.yuceMainFacade.getFridayList(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrHourTimeRespond(result));
    }
}
